package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.Permission;
import cn.wolfcode.domain.Role;
import cn.wolfcode.qo.EmployeeQueryObject;
import cn.wolfcode.service.IPermissionService;
import cn.wolfcode.service.IRoleService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private IRoleService roleService;

    @Autowired
    private IPermissionService permissionService;

    @RequestMapping("/list")
    public String list(Model model, EmployeeQueryObject qo){
        PageInfo<Role> pageInfo = roleService.query(qo);
        model.addAttribute("pageInfo",pageInfo);
        return "/role/list";
    }
    //新增/编辑页面(回显)
    @RequestMapping("/input")
    public String input(Model model,Long roleId){
        if (roleId != null){
            //有id，回显
            Role role = roleService.selectOne(roleId);
            model.addAttribute("role",role);
        }
        //左边的下拉框回显
        List<Permission> permissions = permissionService.selectAll();
        model.addAttribute("permissions",permissions);
        return "role/input";//input.ftl
    }

    //保存新增/编辑
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Role role,Long[] ids){
        if (role.getId()!=null){
            roleService.update(role,ids);
        }else {
            roleService.insert(role,ids);
        }
        return "redirect:/role/list";//list.ftl
    }

    //删除：删除角色之后，也需要删除关联表中的数据
    @RequestMapping("/delete")
    public String delete(Long id){
        roleService.delete(id);
        return "redirect:/role/list";
    }
}
